<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="description" content="">
  <meta name="keywords" content="">
  
    <link rel="icon" href="/avatar.jpg">
  
    
  <title>从 EGO 切换到 hexo | 科学君的不科学博客</title>
  <link rel="stylesheet" href="/style.css">
  <link rel="stylesheet" href="/lib/jquery.fancybox.min.css">
  <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
  <script type="text/javascript" src="https://tajs.qq.com/stats?sId=65546304" charset="UTF-8"></script>
  <script>
  (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      if (curProtocol === 'https') {
          bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
      }
      else {
          bp.src = 'http://push.zhanzhang.baidu.com/push.js';
      }
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
  })();
  </script>
</head>

<body>
  <header>
  <div class="header-container">
    <a class='logo' href="/">
      <span>科学君的不科学博客</span>
    </a>
    <ul class="right-header">
      
        <li class="nav-item">
          
            <a href="/" class="item-link">首页</a>
          
        </li>
      
        <li class="nav-item">
          
            <a href="/about" class="item-link">关于</a>
          
        </li>
      
        <li class="nav-item">
          
            <a href="/archives" class="item-link">归档</a>
          
        </li>
      
        <li class="nav-item">
          
            <a href="/tags" class="item-link">标签</a>
          
        </li>
      
        <li class="nav-item">
          
            <a href="/atom.xml" class="item-link">FEED 订阅</a>
          
        </li>
      
    </ul>
  </div>
</header>

  <main id='post'>
  <div class="content">
    <article>
      <section class="content markdown-body">
        <h1>从 EGO 切换到 hexo</h1>
        <div class='post-meta'>
          <i class="fa fa-calendar" aria-hidden="true"></i>
          <time>2018年03月25日</time>
          
          | <i class="fa fa-folder-open-o" aria-hidden="true"></i> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/blog/">blog</a>
  </div>



          
          
          |
          
          <i class="fa fa-tags" aria-hidden="true"></i>
          
          
  <a href="/tags/#emacs" class='tag'>emacs</a>

  <a href="/tags/#前端" class='tag'>前端</a>

  <a href="/tags/#python" class='tag'>python</a>


          
        </div>
        <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>我刚刚打算写一篇博文，结果发现我的 ego 坏了。回想起来我过去已经在这上面浪费了<br>太多的时间，干嘛不直接切换到更成熟的方案呢_(:3」∠)_。更何况相比于 org，我现在<br>markdown写的更多了。于是我花了两个小时把原先的博客转换到了hexo里面。</p>
<p>在这里记述一下切换的经过，方便后来想要退坑的人。</p>
<h1 id="批量从-org-转换到-markdown"><a href="#批量从-org-转换到-markdown" class="headerlink" title="批量从 org 转换到 markdown"></a>批量从 org 转换到 markdown</h1><p>我写了几行elisp来搞定这件事。</p>
<p>首先从网上抄来一个函数，稍微改了一下，用来获取当前目录下的所有 org 文件：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">(defun files-in-below-directory (directory)</span><br><span class="line">  &quot;List the .el files in DIRECTORY and in its sub-directories.&quot;</span><br><span class="line">  ;; Although the function will be used non-interactively,</span><br><span class="line">  ;; it will be easier to test if we make it interactive.</span><br><span class="line">  ;; The directory will have a name such as</span><br><span class="line">  ;;  &quot;/usr/local/share/emacs/22.1.1/lisp/&quot;</span><br><span class="line">  (interactive &quot;DDirectory name: &quot;)</span><br><span class="line">  (let (el-files-list</span><br><span class="line">        (current-directory-list</span><br><span class="line">         (directory-files-and-attributes directory t)))</span><br><span class="line">    ;; while we are in the current directory</span><br><span class="line">    (while current-directory-list</span><br><span class="line">      (cond</span><br><span class="line">       ;; check to see whether filename ends in &apos;.el&apos;</span><br><span class="line">       ;; and if so, add its name to a list.</span><br><span class="line">       ((equal &quot;.org&quot; (substring (car (car current-directory-list)) -4))</span><br><span class="line">        (setq el-files-list</span><br><span class="line">              (cons (car (car current-directory-list)) el-files-list)))</span><br><span class="line">       ;; check whether filename is that of a directory</span><br><span class="line">       ((eq t (car (cdr (car current-directory-list))))</span><br><span class="line">        ;; decide whether to skip or recurse</span><br><span class="line">        (if</span><br><span class="line">            (equal &quot;.&quot;</span><br><span class="line">                   (substring (car (car current-directory-list)) -1))</span><br><span class="line">            ;; then do nothing since filename is that of</span><br><span class="line">            ;;   current directory or parent, &quot;.&quot; or &quot;..&quot;</span><br><span class="line">            ()</span><br><span class="line">          ;; else descend into the directory and repeat the process</span><br><span class="line">          (setq el-files-list</span><br><span class="line">                (append</span><br><span class="line">                 (files-in-below-directory</span><br><span class="line">                  (car (car current-directory-list)))</span><br><span class="line">                 el-files-list)))))</span><br><span class="line">      ;; move to the next filename in the list; this also</span><br><span class="line">      ;; shortens the list so the while loop eventually comes to an end</span><br><span class="line">      (setq current-directory-list (cdr current-directory-list)))</span><br><span class="line">    ;; return the filenames</span><br><span class="line">    el-files-list))</span><br></pre></td></tr></table></figure></p>
<p>定位到保存 org 文件的目录，做一下转换：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">(cl-loop for file in (files-in-below-directory</span><br><span class="line">                      &quot;d:/TOTALCMD/tools/emacs/home/documents/blog/blog/&quot;)</span><br><span class="line">         do</span><br><span class="line">         (when file</span><br><span class="line">           (with-current-buffer (find-file-noselect file)</span><br><span class="line">             (org-export-to-file &apos;gfm (concat file &quot;.md&quot;)))))</span><br></pre></td></tr></table></figure></p>
<p>我这里用的是 <code>org-gfm-export-to-markdown</code> 函数里的转换方法，这种格式的 markdown<br>更合我胃口。</p>
<p>然后我们就得到了一个混装着 <code>.org</code> 和 <code>.org.md</code> 的文件夹。</p>
<h1 id="从org获取元信息"><a href="#从org获取元信息" class="headerlink" title="从org获取元信息"></a>从org获取元信息</h1><p>光有文章显然是不行的，我们还是得获取一下文章标题、日期、tag之类的东西。我写了一个<br>python脚本来干这件事。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 装着md和org的目录</span></span><br><span class="line">dirPath = <span class="string">"d:/Temp/blog-p/"</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> strftime</span><br><span class="line">now = strftime(<span class="string">"%Y/%m/%d %H:%M:%S"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 生成的 category</span></span><br><span class="line">category = <span class="string">'blog'</span></span><br><span class="line"></span><br><span class="line">fileList = os.listdir(dirPath)</span><br><span class="line"><span class="keyword">for</span> item <span class="keyword">in</span> fileList:</span><br><span class="line">    <span class="keyword">if</span> item.endswith(<span class="string">'.md'</span>):</span><br><span class="line">        <span class="keyword">with</span> open(dirPath + item, <span class="string">'r+'</span>, encoding=<span class="string">'utf-8'</span>) <span class="keyword">as</span> out, open(dirPath + item[:<span class="number">-3</span>], encoding=<span class="string">'utf-8'</span>) <span class="keyword">as</span> inP:</span><br><span class="line">            content = out.read()</span><br><span class="line">            orgAll = inP.readlines()</span><br><span class="line">            <span class="keyword">global</span> title, tags, date, keywords</span><br><span class="line">            keywords = []</span><br><span class="line">            <span class="keyword">for</span> line <span class="keyword">in</span> orgAll:</span><br><span class="line">                <span class="keyword">if</span> line.startswith(<span class="string">'#+TITLE:'</span>):</span><br><span class="line">                    title = line</span><br><span class="line">                <span class="keyword">elif</span> line.startswith(<span class="string">'#+DATE:'</span>):</span><br><span class="line">                    date = line</span><br><span class="line">                <span class="keyword">elif</span> line.startswith(<span class="string">'#+TAGS:'</span>):</span><br><span class="line">                    tags = line</span><br><span class="line">                <span class="keyword">elif</span> line.startswith(<span class="string">'#+KEYWORDS:'</span>):</span><br><span class="line">                    keywords = line[<span class="number">11</span>:<span class="number">-1</span>].strip().split(<span class="string">', '</span>)</span><br><span class="line">            title = title[<span class="number">8</span>:<span class="number">-1</span>].strip()</span><br><span class="line">            date = date[<span class="number">7</span>:<span class="number">-1</span>].strip().split(<span class="string">' '</span>)[<span class="number">0</span>]</span><br><span class="line">            tags = tags[<span class="number">7</span>:<span class="number">-1</span>].strip().split(<span class="string">', '</span>)</span><br><span class="line">            tags = list(set(tags + keywords))</span><br><span class="line">            header = <span class="string">'---\n'</span></span><br><span class="line">            header += <span class="string">'date: '</span> + date.replace(<span class="string">'-'</span>,<span class="string">'/'</span>) + <span class="string">' 00:00:00\n'</span></span><br><span class="line">            header += <span class="string">'updated: '</span> + now + <span class="string">'\n'</span></span><br><span class="line">            header += <span class="string">'tags:\n'</span></span><br><span class="line">            <span class="keyword">for</span> tag <span class="keyword">in</span> tags:</span><br><span class="line">                header += <span class="string">'- '</span> + tag + <span class="string">'\n'</span></span><br><span class="line">            header += <span class="string">'categories:\n- '</span> + category + <span class="string">'\n'</span></span><br><span class="line">            header += <span class="string">'title: '</span> + title + <span class="string">'\n'</span></span><br><span class="line">            header += <span class="string">'---\n'</span></span><br><span class="line"></span><br><span class="line">            out.seek(<span class="number">0</span>, <span class="number">0</span>)</span><br><span class="line">            out.write(header + <span class="string">'\n'</span> + content)</span><br></pre></td></tr></table></figure>
<p>你可以视情况改一下里面的几个变量。由于我以前还用过 org-page，这些文件里面有的文件<br>只有 <code>Keywords</code> 没有 <code>Tags</code>，正好趁这个机会合并一下。</p>
<h1 id="修改其他内容"><a href="#修改其他内容" class="headerlink" title="修改其他内容"></a>修改其他内容</h1><p>比如说org里的图片什么的，由于我也没有几张，就 ag 出来手动改了。</p>

      </section>
      <div class="license">
        <a href="http://creativecommons.org/licenses/by-sa/3.0/" rel="license" target="_blank">
          <img src="http://i.creativecommons.org/l/by-sa/3.0/88x31.png" style="border-width:0"
               alt="Creative Commons License" class="center">
        </a>
      </div>
      <p>
        本作品采用
        <a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" target="_blank">
          署名-相同方式共享 4.0 国际
        </a>
        进行许可。欢迎转载、使用、重新发布，但务必保留文章署名
        “不科学的科学君” (Liu233w) 与博客链接：
        <a href="https://liu233w.github.io">https://liu233w.github.io</a>
        ，基于本文修改后的作品务必以相同的许可发布。如有任何疑问，请
        <a href="mailto:wwwlsmcom@outlook.com" target="_blank">与我联系</a>
        。
      </p>
    </article>
    
    <!-- disqus 评论框 start -->
    <div class="comment">
      <div id="disqus_thread" class="disqus-thread">
        <i>加载评论框需要翻墙</i>
      </div>
    </div>
    <!-- disqus 评论框 end -->
    
    
    <!-- livere 评论框 start -->
    <div class="comment">
      <div id="lv-container" data-id="city" data-uid="MTAyMC8zNTE5NS8xMTczMA=="></div>
    </div>
    <!-- livere 评论框 end -->
    
  </div>
  <aside>
    
    <div class="toc-container">
      <h1>目录</h1>
      <div class="content">
        <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#前言"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#批量从-org-转换到-markdown"><span class="toc-number">2.</span> <span class="toc-text">批量从 org 转换到 markdown</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#从org获取元信息"><span class="toc-number">3.</span> <span class="toc-text">从org获取元信息</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#修改其他内容"><span class="toc-number">4.</span> <span class="toc-text">修改其他内容</span></a></li></ol>
      </div>
    </div>
    
  </aside>
</main>

<!-- disqus 公共JS代码 -->
<script type="text/javascript">
  /* * * CONFIGURATION VARIABLES * * */
  var disqus_shortname = "liu233w";
  var disqus_identifier = "https://liu233w.github.io/2018/03/25/switch-to-hexo/";
  var disqus_url = "https://liu233w.github.io/2018/03/25/switch-to-hexo/";

  isAgent(getDisqus)

  // determine user agent in China
  function isAgent(cb) {
    var url = '//graph.facebook.com/feed?callback=h';
    var xhr = new XMLHttpRequest();
    var called = false;
    xhr.open('GET', url);
    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4 && xhr.status === 200) {
        called = true;
        cb(true);
      }
    };
    xhr.send();
    // timeout 1s, this facebook API is very fast.
    setTimeout(function () {
      if (!called) {
        xhr.abort();
        cb(false)
      }
    }, 1000);
  }

  function getDisqus(isAgent) {
    var dsq = document.createElement('script');
    dsq.type = 'text/javascript';
    dsq.async = true
    dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq)
  }
</script>
<!-- disqus 公共JS代码 end -->


<script type="text/javascript">
  (function (d, s) {
    var j, e = d.getElementsByTagName(s)[0];

    if (typeof LivereTower === 'function') {
      return;
    }

    j = d.createElement(s);
    j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
    j.async = true;

    e.parentNode.insertBefore(j, e);
  })(document, 'script');
</script>


  <footer>
  <div class="copyright">
    <div>
      &copy; 2019 | Powered by <a href="https://hexo.io" target="_blank">Hexo</a>&nbsp
    </div>
    <div>
      Theme by <a href="https://github.com/lewis-geek/hexo-theme-Aath" target="_blank">Aath</a>
    </div>
  </div>
</footer>


<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script src="/lib/in-view.min.js"></script>
<script src="/lib/lodash.min.js"></script>
<script>
  var isDown = true
  var oldY = 0
  inView.offset(50)

  document.body.addEventListener('touchstart', function(){});
  
  window.addEventListener('scroll', _.throttle(e => {
    var currentY = window.scrollY
    if((oldY - currentY) < 0) {
      isDown = true
    } else {
      isDown = false
    }
    oldY = currentY
  }, 250))

  $("article img").each(function() {
      var strA = "<a data-fancybox='gallery' href='" + this.src + "'></a>";
      $(this).wrapAll(strA);
  });

  $('.toc-link').each(function() {
      var href = $(this).attr("href");
      
      inView(href).on('exit', () => {
        if (isDown) {
          handleActive(href)
        }
      })

      inView(href).on('enter', () => {
        if (!isDown) {
          handleActive(href)
        }
      })

      this.onclick = function(e) {
        var pos = $(href).offset().top - 10;
        $("html,body").animate({scrollTop: pos}, 300);
        setTimeout(() => {
          handleActive(href)
        }, 350)
        return false
      }
  })

  function handleActive(href) {
    document.querySelectorAll('.toc-link').forEach(elm => {
      elm.classList.remove('active')
    })
    document.querySelector(".toc [href='"+ href +"']").classList.add('active')
  }
</script>
<script src="/lib/jquery.fancybox.min.js"></script>

<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

</body>
</html>
